package com.snifferx.services.impl;

import java.util.Collection;
import java.util.ResourceBundle;

import org.springframework.security.core.GrantedAuthority;

import com.snifferx.dao.AuthorityDao;
import com.snifferx.domain.AuthorityEntity;
import com.snifferx.services.RolePermissionService;

public class RolePermissionServiceImpl implements RolePermissionService {
	private AuthorityDao authorityDao;
	@Override
	public AuthorityEntity loadAuthorityEntityByUsername(String userName) throws Exception{
		AuthorityEntity authority = authorityDao.loadAuthorityByUserName(userName);
		if (authority == null) {
			throw new Exception(String.format(getMessageBundle().getString("noRole"), userName));
		}
		// TODO :allow user can upgrade to high level when he reach special condition.
		return authority;
	}

	@Override
	public Collection<AuthorityEntity> getAllroles() {
		Collection<AuthorityEntity>  authorityEntities= authorityDao.findAll();
		return authorityEntities;
	}

	@Override
	public boolean updateRole(AuthorityEntity authority) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public Collection<Object> getAllrolesJoinView() {
		Collection<Object>  values = authorityDao.findAllJoin();
		return values;
	}
	
	protected ResourceBundle getMessageBundle() {
		return ResourceBundle.getBundle("message");
	}

	public AuthorityDao getAuthorityDao() {
		return authorityDao;
	}

	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}



}
